package com.mengxuegu.member.controller;


import com.mengxuegu.member.base.Result;
import com.mengxuegu.member.entity.Staff;
import com.mengxuegu.member.req.StaffREQ;
import com.mengxuegu.member.service.IStaffService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 十一捉一
 * @since 2020-12-01
 */
@RestController
@RequestMapping("/staff")
public class StaffController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IStaffService staffService;

    @PostMapping("/list/search/{page}/{size}")
    public Result search(@PathVariable("page") long page,
                         @PathVariable("size") long size,
                         @RequestBody StaffREQ req) {
        logger.info("查询员工信息列表：page={}, size={}", page, size);
        return staffService.search(page, size, req);
    }

    @PostMapping
    public Result add(@RequestBody Staff staff) {
        return staffService.add(staff);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        boolean b = staffService.removeById(id);
        if(b) return Result.ok();
        return Result.error("删除员工信息失败");
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable("id") Integer id) {
         Staff staff = staffService.getById(id);
         // TODO 啊这
//        就算加密了，也不能随便把密码返回过去吧。。我来加个设密码为空。
        staff.setPassword(null);
         return Result.ok(staff);
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Integer id,
                         @RequestBody Staff staff) {
        return staffService.update(id, staff);
    }

}
